package action;
import dao.*;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ProblemListAction extends ActionSupport{
	private int currentPageNo=1;
	private String tip;
	
	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}
	
	public int getCurrentPageNo() {
		return currentPageNo;
	}
	
	public void setTip(String tip) {
		this.tip = tip;
	}
	
	public String getTip() {
		return tip;
	}
	
	@Override
	public String execute() throws Exception {
		//得到request对象
		ActionContext ac = ActionContext.getContext();        
		HttpServletRequest request=(HttpServletRequest)ac.get(ServletActionContext.HTTP_REQUEST);
		ProblemDAO dao=new ProblemDAO();
		int pageSize=30;
		int recordCount=dao.findAll().size();
		int pageCount=0;
		if(recordCount%pageSize==0)
			pageCount=recordCount/pageSize;
		else
			pageCount=recordCount/pageSize+1;
	    if(currentPageNo<1)currentPageNo=1; 
	    System.out.println("currentPageNo="+currentPageNo);
	    //request.setAttribute("currentPageNo", currentPageNo);
	    request.setAttribute("recordCount", recordCount);
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("pageSize", pageSize);
		
		List problem=dao.findCurrentPage(pageSize, currentPageNo);
	    if(!problem.isEmpty()){
	    	System.out.print("得到题目数："+problem.size());
	    	request.setAttribute("problemList",problem);
	    	return SUCCESS;
	    }
		else{
			addActionError("空表。。。。。");
			return INPUT;
		}	
	}	
}